package com.mright.dao;

import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("daoSupport")
public class DaoSupport implements DAO {

	@Autowired
    private SqlSessionTemplate sqlSessionTemplate;

    /**
     * 保存对象
     * @param opr
     * @param obj
     * @return
     * @throws Exception
     */
    public Object save(String opr, Object obj) throws Exception {
        return sqlSessionTemplate.insert(opr, obj);
    }

    /**
     * 批量更新
     * @param opr
     * @param obj
     * @return
     * @throws Exception
     */
    public Object batchSave(String opr, List objs )throws Exception{
        return sqlSessionTemplate.insert(opr, objs);
    }

    /**
     * 修改对象
     * @param opr
     * @param obj
     * @return
     * @throws Exception
     */
    public Object update(String opr, Object obj) throws Exception {
        return sqlSessionTemplate.update(opr, obj);
    }

    /**
     * 批量更新
     * @param opr
     * @param obj
     * @return
     * @throws Exception
     */
    public void batchUpdate(String opr, List objs )throws Exception{
        SqlSessionFactory sqlSessionFactory = sqlSessionTemplate.getSqlSessionFactory();
        //批量执行器
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH,false);
        try{
            if(objs!=null){
                for(int i=0,size=objs.size();i<size;i++){
                    sqlSession.update(opr, objs.get(i));
                }
                sqlSession.flushStatements();
                sqlSession.commit();
                sqlSession.clearCache();
            }
        }finally{
            sqlSession.close();
        }
    }

    /**
     * 批量更新
     * @param opr
     * @param obj
     * @return
     * @throws Exception
     */
    public Object batchDelete(String opr, List objs )throws Exception{
        return sqlSessionTemplate.delete(opr, objs);
    }

    /**
     * 删除对象
     * @param opr
     * @param obj
     * @return
     * @throws Exception
     */
    public Object delete(String opr, Object obj) throws Exception {
        return sqlSessionTemplate.delete(opr, obj);
    }

    /**
     * 查找对象
     * @param opr
     * @param obj
     * @return
     * @throws Exception
     */
    public Object findForObject(String opr, Object obj) throws Exception {
        return sqlSessionTemplate.selectOne(opr, obj);
    }

    /**
     * 查找对象
     * @param opr
     * @param obj
     * @return
     * @throws Exception
     */
    public Object findForList(String opr, Object obj) throws Exception {
        return sqlSessionTemplate.selectList(opr, obj);
    }

    public Object findForMap(String opr, Object obj, String key, String value) throws Exception {
        return sqlSessionTemplate.selectMap(opr, obj, key);
    }
}
